#include <head.h>
int main(int argc, char const *argv[])
{
    pid_t pid = 0;
    if (-1 == (pid = fork()))
    {
        perror("fork error");
        return 0;
    }
    else if (0 < pid)
    {
        exit(1);
    }
    // 创建新会话
    if (-1 == setsid())
    {
        perror("setsid error");
        return 0;
    }
    // 将当前工作目录改为根目录
    if (-1 == chdir("/"))
    {
        perror("chdir error");
        return 0;
    }
    // 重置文件权限掩码
    umask(0);
    // 关闭文件描述符
    fclose(stdout);
    fclose(stdin);
    fclose(stderr);
    // 守护进程任务 写死循环
    while (1)
    {
        FILE *fp = fopen("/tmp/HQYJ.txt", "a");
        time_t t1 = time(NULL);
        struct tm *tm_str;
        while (1)
        {
            t1 = time(NULL);
            tm_str = localtime(&t1);
            fprintf(fp, "HQYJ_log : %2d:%2d:%2d\n", tm_str->tm_hour, tm_str->tm_min, tm_str->tm_sec);
            fflush(fp);
            sleep(10);
        }
    }

    return 0;
}
